import random

def generate_board(size):
    return [[' ' for _ in range(size)] for _ in range(size)]

def place_mines(board, first_move, num_mines):
    size = len(board)
    mines = set()
    while len(mines) < num_mines:
        x, y = random.randint(0, size - 1), random.randint(0, size - 1)
        if (x, y) not in mines and (x, y) != first_move and abs(x - first_move[0]) > 1 and abs(y - first_move[1]) > 1:
            mines.add((x, y))
            board[x][y] = '*'
    return board

def calculate_numbers(board):
    size = len(board)
    for x in range(size):
        for y in range(size):
            if board[x][y] == '*':
                continue
            mine_count = 0
            for dx in range(-1, 2):
                for dy in range(-1, 2):
                    nx, ny = x + dx, y + dy
                    if 0 <= nx < size and 0 <= ny < size and board[nx][ny] == '*':
                        mine_count += 1
            board[x][y] = ' ' if mine_count == 0 else str(mine_count)

def print_board(board, revealed):
    size = len(board)
    print("   " + " ".join(f"{i+1:2}" for i in range(size)))
    print("   +" + "---" * size + "+")
    for x in range(size):
        print(f"{x+1:2} |", end="")
        for y in range(size):
            if revealed[x][y]:
                print(f"{board[x][y]:2}", end=" ")
            else:
                print("■ ", end=" ")
        print("|")
    print("   +" + "---" * size + "+")

def reveal(board, revealed, x, y):
    size = len(board)
    if not (0 <= x < size and 0 <= y < size) or revealed[x][y]:
        return
    revealed[x][y] = True
    if board[x][y] == ' ':
        for dx in range(-1, 2):
            for dy in range(-1, 2):
                nx, ny = x + dx, y + dy
                if 0 <= nx < size and 0 <= ny < size:
                    reveal(board, revealed, nx, ny)

def check_win(board, revealed):
    size = len(board)
    for x in range(size):
        for y in range(size):
            if board[x][y] != '*' and not revealed[x][y]:
                return False
    return True